<template>
  <div>
    <div id="write">
      <div class="head">
        <a-input v-model="way"></a-input>
        <div style="display: inline-block">
          <label for="title">标题</label>
          <a-input type="text" id="title" v-model="title" required />
        </div>
        <button @click="cancelWrite">取消</button>
      </div>
      <mavon-editor
        v-model="editContent"
        ref="md"
        :codeStyle="codeStyle"
        @save="save"
        :ishljs="true"
        class="mavonWrite"
      />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      content: "",
      editContent: "",
      codeStyle: "vs2015",
      way: "vue",
      img: "",
      title: "",
      id: 0,
      text: "添加成功",
      showtip: false,
      msgWay: "success",
    };
  },
  methods: {
    cancelWrite() {
      if (this.$route.path === "/blog") {
        this.$emit("cancelEdit");
      } else {
        this.$router.replace("/blog");
      }
    },
    save(value, render) {
      if (this.$route.path === "/write") {
        this.$axios
          .post("/blog/add", {
            status: this.$store.state.user.userInfo.status,
            content: render,
            editContent: value,
            way: this.way,
            title: this.title,
            username: this.$store.state.user.userInfo.username,
            realname:this.$store.state.user.userInfo.realname
          })
          .then((res) => {
            if (res.data.error == -1) {
              this.$message.success("添加成功");
              this.$parent.showWrite = false;
              this.$router.push("/blog");
            } else {
              this.$message.error("添加失败");
            }
          });
      } else if (this.$route.path === "/blog") {
        console.log(render);
        this.$axios
          .post("/blog/update", {
            status: this.$store.state.user.userInfo.status,
            content: render,
            editContent: value,
            id: this.id,
            title: this.title,
            way: this.way,
          })
          .then((res) => {
            if (res.data.error == -1) {
              this.$message("更新成功");
              this.$parent.showWrite = false;
              this.$parent.getBlogList();
            } else {
              this.$message("更新失败");
            }
          });
      }
    },
  },
};
</script>

<style lang="less" scoped>
// @import url("../../css/comment/animate.css");
.head {
  text-align: center;
  padding: 15px 20px;
  button {
    width: 100px;
    height: 30px;
    border-radius: 10px;
    background-color: #f56c6c;
    margin-left: 15px;
    border: none;
    color: #000;
    transition: all 0.3s;
    &:hover {
      color: #fff;
      background-color: red;
    }
  }
}

#write {
  margin: 0px auto;
  top: 0;

  width: 90%;
  height: calc(100vh - 120px);
  border-radius: 10px;
  position: relative;
  z-index: 9;
  .mavonWrite {
    height: 90%;
    /deep/ p {
      text-indent: 2em;
    }
  }
}
input {
  width: 200px;
  height: 30px;
  margin-left: 15px;
  line-height: 30px;
  padding: 0 15px;
}
label {
  margin-left: 15px;
}
select {
  width: 100px;
  height: 30px;
  border-radius: 10px;
  padding-left: 15px;
  background-color: #e5e5e5;
  margin-left: 15px;
  cursor: pointer;
}
.msg {
  z-index: 9999;
}
@media screen and (max-width: 765px) {
  #write {
    padding: 0;
    margin: 0;
    width: 100%;
  }
  .head {
    width: 100%;
    margin: 15px 0;
  }
  input {
    width: 150px;
  }
}
</style>
